package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Transactional
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper,Appointment> implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(qo.getCustomerName()),"customer_name",qo.getCustomerName());
        wrapper.like(StringUtils.hasText(qo.getCustomerPhone()),"customer_phone",qo.getCustomerPhone());
        wrapper.eq(true,"is_del",0);
        wrapper.eq(qo.getStatus() !=null,"status",qo.getStatus());
        return new TablePageInfo<Appointment>(appointmentMapper.selectList(wrapper));
        
    }


    @Override
    public void updateAppointment(Appointment appointment) {
        //只有预约才能进行编辑
        Appointment oldAppointment = this.getById(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            throw new BusinessException("只有处于预约才能进行编辑操作");
        }
        //UpdateWrapper进行更新条件筛选
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",appointment.getId());
        wrapper.set("customer_name",appointment.getCustomerName());
        wrapper.set("customer_phone",appointment.getCustomerPhone());
        wrapper.set("appointment_time",appointment.getAppointmentTime());
        wrapper.set("license_plate",appointment.getLicensePlate());
        wrapper.set("car_series",appointment.getCarSeries());
        wrapper.set("service_type",appointment.getServiceType());
        wrapper.set("info",appointment.getInfo());
        //update第一个参数为null表示不进行表中所有数据更新
        appointmentMapper.update(null,wrapper);
    }


    @Override
    public void cancel(Long id) {
        //只有预约才能进行取消功能操作
        Appointment oldAppointment = this.getById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            throw new BusinessException("只有处于预约才能进行编辑操作");
        }
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",Appointment.STATUS_CANCEL);
        appointmentMapper.update(null,wrapper);
    }

    @Override
    public void arrival(Long id) {
        //只有预约才能进行到店功能操作
        Appointment oldAppointment = this.getById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            throw new BusinessException("只有处于预约才能进行编辑操作");
        }
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",Appointment.STATUS_ARRIVAL);
        wrapper.set("actual_arrival_time",new Date());
        appointmentMapper.update(null,wrapper);
    }

    public void del(Long id) {
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.ge("id",id);
        wrapper.set("is_del",1);
        appointmentMapper.update(null,wrapper);
    }

    @Override
    @Transactional
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = this.getById(appointmentId);
        //判断是否为到店或结算已生成中，否则无法进行生成结算单或者服务结算单
        if (!Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) &&
                !Appointment.STATUS_SETTLE.equals(appointment.getStatus())) {
            throw new BusinessException("该操作不是处于到店与结算已生成的操作");
        }
        //根据预约单的id去查是否生成过结算单，如果无则之前没有生成过结算单
        Statement newStatement = new Statement();

        QueryWrapper<Statement> statementQueryWrapper = new QueryWrapper<>();
        statementQueryWrapper.eq("appointment_id",appointmentId);
        Statement statement = statementMapper.selectOne(statementQueryWrapper);

        if (statement == null) {
            /**
             * 把预约单信息封装成结算单对象,保存结算单对象
             * 把预约单状态修改成消费中.
             * 返回结算单ID
             */
            //客户名称
            newStatement.setCustomerName(appointment.getCustomerName());
            //客户联系号码
            newStatement.setCustomerPhone(appointment.getCustomerPhone());
            //客户到店时间
            newStatement.setActualArrivalTime(appointment.getActualArrivalTime());
            //客户车牌号
            newStatement.setLicensePlate(appointment.getLicensePlate());
            //客户汽车类型
            newStatement.setCarSeries(appointment.getCarSeries());
            //客户选择服务类型
            newStatement.setServiceType(Long.valueOf(appointment.getServiceType()));
            //传递客户预约id
            newStatement.setAppointmentId(appointment.getId());
            //客户通过预约生成的结算单改为正在消费中状态
            newStatement.setStatus(Statement.STATUS_CONSUME);
            //把预约单封装的数据传递给数据库中的结算单表中
            statementMapper.insert(newStatement);
            //这时预约单状态的改为已生成结算单
            UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",appointmentId);
            wrapper.set("status",Appointment.STATUS_SETTLE);
            appointmentMapper.update(null,wrapper);
            return newStatement;
        }
        //在表中已查询到
        return statement;
    }
}
